package com.bill99.golden.inf.sso.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bill99.golden.inf.sso.mdp.dto.DestroyTokenRequestDto;
import com.bill99.golden.inf.sso.service.CheckClientTokenTimeoutTask;
import com.bill99.golden.inf.sso.service.ClientTokenService;
import com.bill99.golden.inf.sso.session.SessionService;
import com.bill99.golden.inf.sso.util.CM;

/**
 * Servlet implementation class Login
 */
public class Logoutervlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(Logoutervlet.class);
	private static CheckClientTokenTimeoutTask task = null;

	/**
	 * Default constructor.
	 */
	public Logoutervlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>99BILL LOGOUT SERVLET</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print("99BILL LOGOUT SERVLET");
		out.println(", using the GET method");
		out.println("<p>缓存本地的SSO对象总数为：<font color='red'><b>"
				+ ClientTokenService.getTotalBufferedSSOCount()
				+ "</b></font></p>");
		out.println("<p>缓存本地的session总数为：<font color='red'><b>"
				+ SessionService.getLocalSessionCount() + "</b></font></p>");
		out.println("<p>" + new java.util.Date() + "</p>");
		out.println("<p>缓存本地session中的对象总数为：<font color='red'><b>"
				+ SessionService.getTotalSessionObjectCount()
				+ "</b></font></p>");
		out.println("<p>" + new java.util.Date() + "</p>");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String cmd = req.getParameter("cmd");
		if ("logout".equals(cmd)) {
			String token = CM.stringNoNull(req.getParameter("token"));
			DestroyTokenRequestDto r = new DestroyTokenRequestDto();
			r.setAppCode(CM.stringNoNull(req.getParameter("appCode")));
			r.setToken(token);
			r.setUserIp(CM.stringNoNull(req.getParameter("userIp")));
			r.setExplorerType(CM.stringNoNull(req.getParameter("explorerType")));
			ClientTokenService.destroyTokenFromBuffer(r);
			SessionService.destroySession(token);
		} else {
			log.error("Logoutervlet操作命令类型错误：cmd=" + cmd);
		}
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		task = new CheckClientTokenTimeoutTask();
		task.start();
		System.out.println("###Logoutervlet..----------......................");

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		CheckClientTokenTimeoutTask.setRun(false);
		if (task != null) {
			task.interrupt();
		}
		log.info("Logoutervlet destroy OK...........");
	}
}
